package com.yunhe.sms.dao.system.impl;


import com.yunhe.commons.dao.impl.BaseDaoImpl;
import com.yunhe.commons.dto.Pageable;
import com.yunhe.sms.dao.system.UserDao;
import com.yunhe.sms.entity.system.User;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/14 0014.
 */
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
    public static void main(String[] args) throws SQLException {
        UserDaoImpl userDao = new UserDaoImpl();
       /* User user = new User(null,"006","teacher3","teacher456","王五",1,3);
        save(user);*/
       /*del(42);*/

       /* User user = new User(46,"007","teacher3","teacher456","王五",1,3);

        update(user);*/
        userDao.findList();

    }



    @Override
    public  void save(User user){
        String sql = " insert into sys_user(user_name,password,phone,mail,role_id,status,created_time,deleted) values(?,?,?,?,?,?,now(),0) ";
        super.cud(sql,user.getUserName(),user.getPassword(),user.getPhone(),user.getMail(),user.getRoleId(),user.getStatus());
    }


    @Override
    /*public  void delete(Integer id){
        String sql = " delete from sys_user where id = ? ";
        super.cud(sql,id);
    }*/
    public  void delete(Long id){
        String sql = " update sys_user set deleted=1 where id = ? ";
        super.cud(sql,id);
    }


    @Override
    public  void update(User user){
        String sql = " update sys_user set no=?,user_name=?,phone=?,mail=?,role_id=?,status=? where id=? ";
        super.cud(sql,user.getNo(),user.getUserName(),user.getPhone(),user.getMail(),user.getRoleId(),user.getStatus(),user.getId());
    }

    @Override
    public void updatePwd(String password,Long id) {
        String sql = " update sys_user set password = ? where id=? ";
        super.cud(sql,password,id);
    }

    @Override
    public  List<User> findList(){
        String sql = " select id,no,user_name userName,password,phone,mail,role_id roleId,status from sys_user where deleted = 0 order by no asc ";
        List<User> userList = super.queryList(sql);
        return userList;

    }


    @Override
    public  User findOne(Long id){
        String sql = " select id,no,user_name userName,password,phone,mail,role_id roleId,status from sys_user where id = ? and deleted = 0 ";
        User user = super.queryOne(sql,id);
        return user;
        /*System.out.println(user.getType()+"："+user.getUserName()+"："+user.getPwd());*/
    }

    @Override
    public User findOne(String userName, String password) {
        String sql = " select id,no,user_name userName,password,phone,mail,role_id roleId,status from sys_user where user_name = ? and password = ? and deleted = 0 and status = 1 ";
        User user = super.queryOne(sql, userName, password);
        return user;
    }

    @Override
    public  User findOne(String userName){
        String sql = " select id,no,user_name userName,password,phone,mail,role_id roleId,status from sys_user where user_name = ? and deleted = 0 and status = 1 ";
        User user = super.queryOne(sql,userName);
        return user;
        /*System.out.println(user.getType()+"："+user.getUserName()+"："+user.getPwd());*/
    }

    @Override
    public Pageable<User> findPage(Integer offset, Integer size) {
/*
        String listSql = " select id,no,user_name userName,password,phone,mail,role_id roleId,status from sys_user where deleted = 0 ";
*/
        String listSql = " select u.id,u.user_name userName,u.phone,u.mail,u.role_id roleId,u.status,r.name roleName from sys_user u left join sys_role r on r.deleted = 0 and r.`status` = 1 and u.role_id = r.id where u.deleted = 0 ";

        String countSql = " select count(*) from sys_user where deleted = 0 ";

        Pageable<User> pageable = super.queryPage(listSql, countSql, offset, size);
        return pageable;

    }





    /**
     * 查询用户的权限信息
     * @param roleId
     * @return
     * @throws SQLException
     */
   /* @Override
    public List<Permi> findPermiByRoleId(Integer roleId){
       String sql = " select p.* from permi p,role r,role_permi rp where p.id = rp.permin_id and r.id = rp.role_id and r.id = ? ";

        *//*List<Permi> permi = super.queryList(sql, new BeanListHandler<Permi>(Permi.class), roleId);*//*

        return permi;

    }*/

}
